package com.luck.picture.lib.tools;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaRouter;
import android.media.SoundPool;
import android.util.Log;

import com.luck.picture.lib.R;

import java.io.FileNotFoundException;
import java.io.FileReader;

/**
 * author：luck
 * project：PictureSelector
 * package：com.luck.picture.lib.tool
 * email：893855882@qq.com
 * data：2017/5/25
 */

public class VoiceUtils {
    private static SoundPool soundPool;
    private static int soundID;//创建某个声音对应的音频ID

    /**
     * start SoundPool
     */
    public static void playVoice(Context mContext, boolean enableVoice) {
        if (soundPool == null) {
            soundPool = new SoundPool(1, AudioManager.STREAM_ALARM, 0);
            soundID = soundPool.load(mContext, R.raw.music, 1);
        }
        if (enableVoice) {
            soundPool.play(
                    soundID,
                    0.1f,
                    0.5f,
                    0,
                    1,
                    1
            );
        }
    }

    /**
     * release SoundPool
     */
    public static void release() {
        if (soundPool != null) {
            soundPool.stop(soundID);
        }
        soundPool = null;
    }

    public static boolean isHeadsetExists() {
        String HEADSET_STATE_PATH = "/sys/class/switch/h2w/state";
        char[] buffer = new char[1024];
        int newState = 0;

        try {
            FileReader file = new FileReader(HEADSET_STATE_PATH);
            int len = file.read(buffer, 0, 1024);
            newState = Integer.valueOf((new String(buffer, 0, len)).trim());
        }
        catch (FileNotFoundException e) {
            Log.e("FMTest", "This kernel does not have wired headset support");
        }
        catch (Exception e) {
            Log.e("FMTest", "", e);
        }
        return newState != 0;
    }
}
